package home_code;

import java.util.LinkedList;
import java.util.Queue;

public class PalindromeQueue {
    public static boolean isPalindrome(String word) {
        word = word.toLowerCase(); // 转换为小写字母，忽略大小写
        Queue<Character> queue = new LinkedList<>();

        // 将单词的每个字符依次入队列
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if (Character.isLetter(c)) {
                queue.add(c);
            }
        }

        // 逐个比较队列中的字符和单词中的字符是否相同
        while (!queue.isEmpty()) {
            char front = queue.poll();
            char back = word.charAt(word.length() - 1);
            if (front != back) {
                return false;
            }
            word = word.substring(0, word.length() - 1); // 移除单词的最后一个字符
        }

        return true;
    }

    public static void main(String[] args) {
        String word1 = "level";
        String word2 = "hello";

        System.out.println(word1 + " is palindrome? " + isPalindrome(word1));
        System.out.println(word2 + " is palindrome? " + isPalindrome(word2));
    }
}
